//
// Created by Suzhen on 2016/12/13.
//

#include "Utils.h"

#ifdef LINUX
extern "C"
{
#include <pthread.h>
#include <sched.h>
}

#include <stdexcept>
#endif

USING_ORION_V2

void Utils::CloseUvLoop(uv_loop_t *l) {
    for (;;) {
        //0 close complete! otherwise maybe E_UVBUSY
        if (0 == uv_loop_close(l)) {
            break;
        }
    }
}

std::string Utils::sformat_impl_(boost::format &f) {
    return f.str();//end
}

void Utils::bindThread2CpuByIndex(std::size_t idx) {
#ifdef LINUX
    cpu_set_t cpuset;
    CPU_ZERO( & cpuset);
    CPU_SET( n, & cpuset);
    int errno_( ::pthread_setaffinity_np( ::pthread_self(), sizeof( cpuset), & cpuset) );
    SYS_FAULT(errno_ == 0)
        << "错误,无法将当前线程 " << std::this_thread::get_id() << " 绑定到第 " << cpuIdx + 1 << " 颗CPU核心上";
#endif
}
